layui.use(['table', 'admin', 'ax', 'ztree'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ZTree = layui.ztree;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 系统管理--城市合伙人管理
     */
    var Dept = {
        tableId: "deptTable"
    };

    /**
     * 选择城市合伙人时
     */
    Dept.onClickDept = function (e, treeId, treeNode) {
        Dept.search(treeNode.id);
    };

    /**
     * 初始化表格的列
     */
    Dept.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'deptId', hide: true, sort: true, title: 'id'},
            {field: 'simpleName', sort: true, title: '城市合伙人简称'},
            {field: 'fullName', sort: true, title: '城市合伙人全称'},
            {field: 'sort', sort: true, title: '排序'},
            {field: 'description', sort: true, title: '备注'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 300}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Dept.search = function (deptId) {
        var url = Feng.ctxPath + "/dept/childDept?name=" + $("#deptName").val()
        if (deptId) {
            url += "&deptId=" + deptId
        }
        var ajax = new $ax(url, function (data) {
            table.reload(Dept.tableId, {data: data});
        }, function (data) {
            Feng.error("查询失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    };

    /**
     * 弹出添加
     */
    Dept.openAddDept = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加城市合伙人',
            content: Feng.ctxPath + '/dept/dept_add',
            end: function () {
                admin.getTempData('formOk') && Dept.search()
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Dept.exportExcel = function () {
        var checkRows = table.checkStatus(Dept.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑城市合伙人
     *
     * @param data 点击按钮时候的行数据
     */
    Dept.onEditDept = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改城市合伙人',
            content: Feng.ctxPath + '/dept/dept_update?deptId=' + data.deptId,
            end: function () {
                admin.getTempData('formOk') && Dept.search()
            }
        });
    };
    Dept.logo = function (data) {
        if (data.logo && data.logo != '') {
            layer.photos({
                photos: {
                    "title": "logo", //相册标题
                    "id": 1233, //相册id
                    "start": 0, //初始显示的图片序号，默认0
                    "data": [{
                        "alt": "logo",
                        "pid": '1',
                        "src": "http://childlogo-1258443025.picsh.myqcloud.com/" + data.logo + "?imageView2/rq/70",
                        "thumb": ""
                    }]
                }
            });
        } else {
            Feng.error("没有上传logo");
        }
    };
    Dept.qrCode = function (data) {
        if (data.qrCode && data.qrCode != '') {
            layer.photos({
                photos: {
                    "title": "", //相册标题
                    "id": 1233, //相册id
                    "start": 0, //初始显示的图片序号，默认0
                    "data": [{
                        "alt": '二维码',
                        "pid": 1,
                        "src": "http://childlogo-1258443025.picsh.myqcloud.com/" + data.qrCode + "?imageView2/rq/70",
                        "thumb": ""
                    }]
                }
            });
        } else {
            Feng.error("没有上传二维码");
        }
    };

    /**
     * 点击删除城市合伙人
     *
     * @param data 点击按钮时候的行数据
     */
    Dept.onDeleteDept = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/dept/delete", function (res) {
                if (res && res.code == 200) {
                    Dept.search()
                    Feng.success("删除成功!");
                } else {
                    Feng.error(res.message);
                }
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("deptId", data.deptId);
            ajax.start();
        };
        Feng.confirm("是否删除城市合伙人 " + data.simpleName + "?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Dept.tableId,
        data: [],
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Dept.initColumn()
    });

    Dept.search();

    var ztree = new $ZTree("adeptTree", "/dept/childTree");
    ztree.bindOnClick(Dept.onClickDept);
    ztree.init();

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Dept.search();

    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Dept.openAddDept();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Dept.exportExcel();
    });

    Dept.createUrl = function (data) {
        var ajax = new $ax(Feng.ctxPath + "/dept/url", function (res) {
            if (res.code == 200) {
                layer.open({
                    type: 0,
                    area: '600px',
                    title: '链接',
                    content: '<p>' + res.data + '</p>'
                });
            } else {
                Feng.error("生成链接失败!");
            }
        }, function (data) {
            Feng.error("生成链接失败!");
        });
        ajax.set("deptId", data.deptId);
        ajax.set("route", "welcomepage");
        ajax.start();
    };

    // 工具条点击事件
    table.on('tool(' + Dept.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Dept.onEditDept(data);
        } else if (layEvent === 'delete') {
            Dept.onDeleteDept(data);
        } else if (layEvent === 'logo') {
            Dept.logo(data);
        } else if (layEvent === 'qrCode') {
            Dept.qrCode(data);
        } else if (layEvent === 'createUrl') {
            Dept.createUrl(data);
        }
    });


});
